package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/3/10 14:00
 * Version:1.0-SNAPSHOT
 */
@Controller
//@RestController
@RequestMapping("/response")
public class ResponseController {

    @RequestMapping("/f1")
    public ModelAndView f1(){
        ModelAndView mav = new ModelAndView();
        // 当此处设置viewName的时候
        // 其值的内容与方法返回字符串一致
        // 其值的格式也是三种
        // 1.直接写字符串     将返回的字符串作为视图名存在
        // 2.redirect:访问命令  使用重定向的方式访问指定的命令
        // 3.forward:访问命令   使用转发的方式访问指定的命令
        mav.setViewName("login");
        return mav;
    }

    @RequestMapping("/f2")
    public String f2(){
        // 当方法返回值为字符串时
        // 其字符串的格式有三种
        // 1.直接写字符串     将返回的字符串作为视图名存在
        // 2.redirect:访问命令  使用重定向的方式访问指定的命令
        // 3.forward:访问命令   使用转发的方式访问指定的命令
        return "login";
    }

    @RequestMapping("/f3")
    public String f3(HttpServletRequest request){
        request.setAttribute("msg","Hello SpringMVC");
        return "hello";
    }

    @RequestMapping("/f4")
    public String f4(){
        return "redirect:/showLogin";
    }
    @RequestMapping("/f5")
    public String f5(){
        return "forward:/showLogin";
    }

    @RequestMapping("/f6")
    public ModelAndView f6(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("forward:/showLogin");
        return mav;
    }

    @RequestMapping("/f7")
    public void f7(){
        System.out.println("ResponseController.f7");
        // /WEB-INF/pages/response/f7.jsp
        // /WEB-INF/pages/
        // /response/f7
        // .jsp
        // 将当前的请求名作为视图名返回
    }

    @RequestMapping("/f8")
    public void f8(HttpServletRequest request){
        System.out.println("ResponseController.f8");
        // /WEB-INF/pages/response/f8.jsp
        // 将当前的请求名作为视图名返回
    }

    @RequestMapping("/f9")
    public void f9(HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        System.out.println("ResponseController.f8");
        // 没有响应
        PrintWriter out = response.getWriter();
        out.print("<h1>我来响应了</h1>");
    }

    @RequestMapping("/f10")
    public void f10(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/showLogin").forward(request,response);
    }

    @RequestMapping("/f11")
    public void f11(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect(request.getContextPath()+"/showLogin");
    }

    @ResponseBody
    @RequestMapping("/f12")
    public String f12(){
        return "login";
    }

    @RequestMapping("/f13")
    public @ResponseBody String f13(){
        return "<h1>Hello Ajax</h1>";
    }

    @RequestMapping("/f14")
    public void f14(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username:"+username+",password:"+password);
    }

    @RequestMapping("/f15")
    public ModelAndView f15(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session){
        ModelAndView mav = new ModelAndView();
        System.out.println("username:"+username+",password:"+password);
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://127.0.0.1:3306/ums?useUnicode=true&characterEncoding=utf-8";
        User user = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,"root","");
            String sql = new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .append(" where username = ? ")
                    .append(" and password = ? ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1,username);
            ps.setString(2,password);
            rs = ps.executeQuery();
            while(rs.next()){
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                session.setAttribute("user",user);
                mav.setViewName("redirect:/showMain");
                return mav;
            }
            mav.addObject("loginMsg","用户名或密码错误");
            mav.setViewName("login");
        } catch (Exception e) {
            e.printStackTrace();
            mav.addObject("loginMsg","服务器内部异常");
            mav.setViewName("login");
        }
        return mav;
    }

    // 当满足以下条件时,@RequestParam(key)可以省略
    // 1.当前参数没有被任何注解所标注
    // 2.当前参数不是一个对象
    // 3.当前参数的名字与请求参数中的key一致
    @RequestMapping("/f16")
    public void f16(String username,String password){
        System.out.println("username:"+username+",password:"+password);
    }


    @RequestMapping("/f17")
    public void f17(@RequestParam(value = "username",required = false) String username,@RequestParam(value = "password",required = false) String password){
        System.out.println("username:"+username+",password:"+password);
    }

}
